package com.qyhl.webtv.module_live.teletext.detail;

import android.util.Log;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.launcher.ARouter;
import com.google.gson.Gson;
import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.basiclib.utils.network.model.ApiResult;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.ServicePathConstant;
import com.qyhl.webtv.commonlib.entity.intergral.CoinBean;
import com.qyhl.webtv.commonlib.entity.live.ShoppingListBean;
import com.qyhl.webtv.commonlib.entity.live.TeleTextBean;
import com.qyhl.webtv.commonlib.service.IntergralService;
import com.qyhl.webtv.module_live.common.LiveUrl;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by qyhl on 2017/12/15.
 */

public class TeleTextModel implements TeleTextContract.TeleTextModel {
    private TeleTextPresenter mPresenter;
    @Autowired(name = ServicePathConstant.INTERGRAL_SERICE)
    public IntergralService intergralService;
    public TeleTextModel(TeleTextPresenter mPresenter) {
        this.mPresenter = mPresenter;
        ARouter.getInstance().inject(this);
    }

    /**
     * 获取图文详情
     *
     * @param sceneId
     */
    @Override
    public void getDetail(String sceneId) {
        String userName = CommonUtils.getInstance().getUserName();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url =LiveUrl.GET_TELETEXT_DETAIL + "?timestamp=" + time;

        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "userName=" + userName + "&sceneId=" + sceneId ))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<TeleTextBean>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            mPresenter.setError(2, "暂无任何内容！");
                        } else {
                            mPresenter.setError(1, "解析出错，加载失败！");
                        }
                    }

                    @Override
                    public void onSuccess(TeleTextBean teleTextBean) {
                        mPresenter.setDetail(teleTextBean);
                    }
                });

    }

    /**
     * 点赞
     *
     * @param sceneId
     */
    @Override
    public void getPraise(String sceneId) {

        EasyHttp.get(LiveUrl.GET_TELETEXT_PRAISE)
                .params("sceneId", sceneId)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.praiseError("网络异常，点赞失败！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s,ApiResult.class);
                        if(apiResult.getCode()==200){
                            mPresenter.praiseSuccess(apiResult.getData().toString());
                        }else{
                            mPresenter.praiseError("数据获取出错，点赞失败！");
                        }
                    }
                });

    }

    /**
     * 点击量
     *
     * @param sceneId
     */
    @Override
    public void addClick(String sceneId) {

        EasyHttp.get(LiveUrl.GET_TELETEXT_SCAN)
                .params("sceneId", sceneId)
                .execute(new SimpleCallBack<String>() {

                    @Override
                    public void onError(ApiException e) {

                    }

                    @Override
                    public void onSuccess(String s) {
                        Log.i("success", "图文点击量成功！");
                    }
                });
    }

    //开启提醒
    @Override
    public void dofollow(final String sceneId) {
        String userName = CommonUtils.getInstance().getUserName();

        EasyHttp.get(LiveUrl.GET_TELETEXT_FOLLOW)
                .params("userName", userName)
                .params("sceneId", sceneId)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.followError("网络异常，开启失败！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            mPresenter.followSuccess("已开启开播提醒！", sceneId);
                        } else {
                            mPresenter.followError("解析出错，开启失败！");
                        }
                    }
                });

    }

    //关闭提醒
    @Override
    public void unfollow(final String sceneId) {
        String userName = CommonUtils.getInstance().getUserName();

        EasyHttp.get(LiveUrl.GET_TELETEXT_UNFOLLOW)
                .params("userName", userName)
                .params("sceneId", sceneId)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.unfollowError("网络异常，关闭失败！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            mPresenter.unfollowSuccess("已关闭开播提醒！", sceneId);
                        } else {
                            mPresenter.unfollowError("解析出错，关闭失败！");
                        }
                    }
                });

    }

    @Override
    public void getCoin(String username) {
        intergralService.gainCoin("3","gainCoin", new IntergralService.GainCoinCallBack() {
            @Override
            public void success(CoinBean coinBean) {
                mPresenter.getCoinSuccess(coinBean);
            }

            @Override
            public void error(String msg) {
                mPresenter.getCoinFailure(msg);
            }
        });
    }

    @Override
    public void getShopTop(String sceneId) {
        EasyHttp.get(LiveUrl.GET_TELETEXT_SHOP_TOP)
                .params("sceneId", sceneId)
                .execute(new SimpleCallBack<ShoppingListBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.setShopTop(null);
                    }

                    @Override
                    public void onSuccess(ShoppingListBean shoppingListBean) {
                        mPresenter.setShopTop(shoppingListBean);
                    }
                });
    }
}
